package com.macro.mall.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.ResultCode;
import com.macro.mall.common.exception.Asserts;
import com.macro.mall.common.utils.Assist;
import com.macro.mall.mapper.TiTrainingUserMapper;
import com.macro.mall.model.*;
import com.macro.mall.service.TiTrainingUserService;
import com.macro.mall.utils.Mapping;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * TiTrainingUser的服务接口的实现类
 *
 * @author
 */
@Slf4j
@Service
public class TiTrainingUserServiceImpl implements TiTrainingUserService {

    private boolean isDebugEnabled = true;

    @Autowired
    private TiTrainingUserMapper tiTrainingUserMapper;

    @Override
    public List<TiTrainingUser> find(String username,String phone,String communityName,String userIdentityName,Integer pageNum,Integer pageSize) {
        //TODO这里可以做通过Assist做添加查询
        PageHelper.startPage(pageNum, pageSize);
        Assist assist = new Assist();
        List<TiTrainingUser> result = null;
        try {
            if (StrUtil.isNotEmpty(username)) {
                assist.andLike("username", "%" + username + "%");
            }
            if (StrUtil.isNotEmpty(phone)) {
                assist.andLike("phone", "%" + phone + "%");
            }
            if (StrUtil.isNotEmpty(communityName)) {
                assist.andLike("community_name", "%" + communityName + "%");
            }
            if (StrUtil.isNotEmpty(userIdentityName)) {
                assist.andLike("user_identity_name", "%" + userIdentityName + "%");
            }
            assist.setOrder(Assist.order("create_time",false));
            result = tiTrainingUserMapper.selectTiTrainingUser(assist);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行获取TiTrainingUser数据集-->结果:{}", result);
        }
        return result;
    }

    @Override
    public CommonResult importUserNotNull() {
        int result = 0;
        try {
            List<TiTrainingUser> allUserList = tiTrainingUserMapper.selectAllMember();
            if (!CollectionUtils.isEmpty(allUserList)){
                allUserList.stream().forEach(aul->{
                    aul.setUserIdentityName(Mapping.getSplitName(aul.getUserIdentity()));
                    aul.setCreateTime(new Date());
                });
                result = tiTrainingUserMapper.insertTiTrainingUserByBatch(allUserList);
            }
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行将TiTrainingUser中属性值不为null的数据保存到数据库-->结果:{}", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult manageUserNotNull(TiTrainingUser value,String oper) {
        List<TiTrainingUser> userList = null;
        int result = 0;
        try {
            switch (oper){
                case "01":
                    saveNotNull(value);
                    break;
                case "02":
                    deleteById(value.getId());
                    break;
                case "03":
                    updateNotNullById(value);
                    break;
                case "04":
                    userList = selectByObj(value);
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行将TiTrainingUser中属性值不为null的数据保存到数据库-->结果:{}", result);
        }
        return CommonResult.success(userList);
    }

    @Override
    public List<TiTrainingUser> selectByObj(TiTrainingUser value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行通过TiTrainingUser的id获得TiTrainingUser对象-->失败:id不能为空");
            }
            Asserts.fail("执行通过TiTrainingUser的id获得TiTrainingUser对象-->失败:id不能为空");
        }
        List<TiTrainingUser> result = null;
        try {
            result = tiTrainingUserMapper.selectTiTrainingUserByObj(value);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过TiTrainingUser的id获得TiTrainingUser对象-->结果:{}", result);
        }
        return result;
    }

    @Override
    public CommonResult saveNotNull(TiTrainingUser value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行将TiTrainingUser中属性值不为null的数据保存到数据库-->失败:对象不能为空");
            }
            Asserts.fail("失败:对象不能为空");
        }
        int result = 0;
        try {
            value.setCreateTime(new Date());
            result = tiTrainingUserMapper.insertNotNullTiTrainingUser(value);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行将TiTrainingUser中属性值不为null的数据保存到数据库-->结果:{}", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult updateNotNullById(TiTrainingUser value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行通过TiTrainingUser的id更新TiTrainingUser中属性不为null的数据-->失败:对象为null");
            }
            Asserts.fail("执行通过TiTrainingUser的id更新TiTrainingUser中属性不为null的数据-->失败:对象为null");
        }
        int result = 0;
        try {
            value.setUpdateTime(new Date());
            result = tiTrainingUserMapper.updateNotNullTiTrainingUserById(value);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过TiTrainingUser的id更新TiTrainingUser中属性不为null的数据-->结果:{}", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult deleteById(Integer id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过TiTrainingUser的id删除TiTrainingUser-->失败:id不能为空");
            }
            Asserts.fail("执行通过TiTrainingUser的id删除TiTrainingUser-->失败:id不能为空");
        }
        int result = 0;
        try {
            result = tiTrainingUserMapper.deleteTiTrainingUserById(id);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过TiTrainingUser的id删除TiTrainingUser-->结果:{}", result);
        }
        return CommonResult.success(null);
    }

}